import pygame
from .. import debug

def load(fileOrPath,convert=False,alpha=False):
    """ Loads a surface.
        For fileOrPath, specify either a filename or a file-like object. The
        format of this file may be one of the file formats understood by Pygame.
        For `convert` and `alpha`, specify whether or not you want the
        surface to be converted to the current display format and with or
        without per-pixel alpha enabled. (`alpha` is ignored if `convert` is
        False.)
        Returns a pygame Surface object.
    """
    debug.message("Loading surface '%s'"%fileOrPath)
    if convert:
        if alpha:
            return pygame.image.load(fileOrPath).convert_alpha()
        else:
            return pygame.image.load(fileOrPath).convert()
    else:
        return pygame.image.load(fileOrPath)

def _autoload(agent):
    """ Internal function.
        Autoloader for Pygame surfaces.
    """
    return load(agent.src,
                 agent.extra['convert'] if 'convert' in agent.extra else False,
                 agent.extra['alpha'] if 'alpha' in agent.extra else False)